package com.nondo.rdp.finance.service.impl.course;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.core.common.StatusConstant;
import com.nondo.rdp.finance.domain.course.CourseClassify;
import com.nondo.rdp.finance.repository.course.CourseClassifyRepository;
import com.nondo.rdp.finance.service.course.CourseClassifyService;

@Transactional
@Service
public class CourseClassifyServiceImpl implements CourseClassifyService {
    @Autowired
    CourseClassifyRepository courseClassifyRepository;

    @Override
    public CourseClassify findOne(Long id) {
        return this.courseClassifyRepository.findOne(id);
    }

    @Override
    public CourseClassify save(CourseClassify courseClassify) {
        return this.courseClassifyRepository.save(courseClassify);
    }

    @Override
    public void update(CourseClassify courseClassify) {
        this.courseClassifyRepository.save(courseClassify);
    }
    
    @Override
    public void delete(Long id) {
        this.courseClassifyRepository.deleteById(id);
    }

    @Override
    public Page<CourseClassify> findAll(Page<CourseClassify> page, CourseClassify courseClassify) {
        Sort sort = new Sort(Sort.Direction.DESC, "sort").and( new Sort(Sort.Direction.DESC, "createDate"));
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<CourseClassify> dictPage = courseClassifyRepository.findAll((root, query, builder) -> {
        	List<Predicate> predicates = new ArrayList<>();
        	if (courseClassify != null) {
        		if ( courseClassify.getId()!=null) {
        			predicates.add(builder.equal(root.get("id"), courseClassify.getId()));
        		}
        		if ( courseClassify.getPid()!=null) {
        			predicates.add(builder.equal(root.get("pid"), courseClassify.getPid()));
        		}
        		System.out.println(courseClassify.getType());
        		if ( courseClassify.getType()!=null) {
        			predicates.add(builder.equal(root.get("type"), courseClassify.getType()));
        		}
        		if ( courseClassify.getName()!=null) {
        			predicates.add(builder.like(root.get("name"), "%" + courseClassify.getName() + "%"));
        		}
        	}
            predicates.add(builder.like(root.get("delFlag"), StatusConstant.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
        	
            return builder.and(predicatesArray);
        	
        }, pageRequest);

        return new Page<CourseClassify>(dictPage);
    }

	@Override
	public CourseClassify findByName(String name) {
		return courseClassifyRepository.findByName(name);
	}

	@Override
	public List<CourseClassify> findByPid(Long id) {
		return courseClassifyRepository.findByPid(id);
	}

}